#include "CEnemy.h"
#include "..\\Messaging\CSGP_EventSystem.h"
#include "CPistolBullet.h"
#include "CShotgunBullet.h"
#include "CRocketLauncherBullet.h"
#include "CGrenade.h"
#include "..\\Messaging\CSGP_MessageSystem.h"
#include "CPlayer.h"
#include "..\CTile.h"
#include "..\CGame.h"
#include "..\CCyberSpaceState.h"
#include "..\CSpaceStationState.h"
#include "..\COptionsState.h"
#include "..\CLayer.h"
#include "..\Wrappers\CSGD_TextureManager.h"
#include "CDrop.h"

CEnemy::CEnemy()
{
	SetType(CBase::OBJ_ENEMY);
	m_bScreenFlashing = false;
	m_nFlashCount = 0;

	m_nHitID = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/SGP_Hit.wav");
	// Set the sound volume
	CSGD_FModManager::GetInstance()->SetVolume(m_nHitID, COptionsState::GetInstance()->GetSFXVolume());
	m_nPickUpId = -1;
	drop = NULL;
}

CEnemy::~CEnemy()
{
}

RECT CEnemy::GetCollisionRect(void)
{
	RECT rect;
	rect.left = long(GetPosX());
	rect.top = long(GetPosY());
	rect.right = rect.left + GetWidth();
	rect.bottom = rect.top + GetHeight();

	return rect;
}

void CEnemy::Fire(void)
{

}

void CEnemy::Update(float fElapsedTime)
{
	CBase::Update(fElapsedTime);

	if(health <= 0)
	{
		CPlayer* player = CPlayer::GetInstance();
		switch(GetType())
		{
		case OBJ_WORM:
			player->SetMoney((int)(player->GetMoney() + 50 * (1 + player->GetMoneyFactor())));
			break;
		case OBJ_TURRET:
			player->SetMoney((int)(player->GetMoney() + 100 * (1 + player->GetMoneyFactor())));
			break;
		case OBJ_SPIDER:
			player->SetMoney((int)(player->GetMoney() + 150 * (1 + player->GetMoneyFactor())));
			break;
		}
		CSGP_EventSystem::GetInstance()->SendEvent("enemy.death", this);
		RandomDrop();
	}

	if(GetType() == OBJ_SPIDER || GetType() == OBJ_WORM)
	{
		CNode* node;
		if(CSpaceStationState::GetInstance()->getstate())
		{
			node = CSpaceStationState::GetInstance()->getnode();
		}
		else
			node = CCyberSpaceState::GetInstance()->getnode();
		if(node)
		{
			RECT obj = this->GetCollisionRect();
			std::vector<CLayer*>* layers = node->getlayers();
			for(unsigned int i = 1; i < (*layers).size(); i++)
			{
				std::vector<CTile*>* tiles = (*layers)[i]->GetTiles();
				for(unsigned int j = 0; j < tiles->size(); j++)
				{
					RECT curtile;
					RECT temp;
					curtile.left = tiles->operator[](j)->GetmX() * tiles->operator[](j)->GetWidth();
					curtile.top = tiles->operator[](j)->GetmY() * tiles->operator[](j)->GetHeight();
					curtile.bottom = curtile.top + tiles->operator[](j)->GetHeight();
					curtile.right = curtile.left + tiles->operator[](j)->GetWidth();

					if(IntersectRect(&temp, &obj, &curtile))
					{


						int wid = temp.right - temp.left;
						int het = temp.bottom - temp.top;

						if(wid > het)
						{
							if(obj.bottom > curtile.top && obj.top < curtile.top)
							{
								SetPosY(GetPosY() - (obj.bottom - curtile.top));
								SetVelY(0.0f);
							}
							if(obj.top < curtile.bottom && obj.bottom > curtile.bottom)
							{
								SetPosY(GetPosY() - (obj.top - curtile.bottom));
								SetVelY(0.0f);
							}

						}
						else if(het > wid)
						{
							if(obj.right > curtile.left && obj.left < curtile.left && GetVelX() != 0)
							{
								SetPosX(GetPosX() - (obj.right - curtile.left));
								SetVelX(0.0f);
							}

							if(obj.left < curtile.right && obj.right > curtile.right && GetVelX() != 0)
							{
								SetPosX(GetPosX() - (obj.left - curtile.right));
								SetVelX(0.0f);
							}
						}
						else
						{

						}
					}
				}
			}
		}
	}

	if(m_bScreenFlashing)
	{
		m_nFlashCount++;
		if(m_nFlashCount > 5)
		{
			m_bScreenFlashing = false;
			m_nFlashCount = 0;
		}
	}
}

void CEnemy::Render(void)
{
	if(m_bScreenFlashing)
	{
		RECT rFlash;
		rFlash.left = 0;
		rFlash.top = 0;
		rFlash.right = rFlash.left + CGame::GetInstance()->GetScreenWidth();
		rFlash.bottom = rFlash.top + CGame::GetInstance()->GetScreenHeight();

		CSGD_Direct3D::GetInstance()->DrawRect(rFlash, 255, 255, 255);
	}
	CSGD_Direct3D::GetInstance()->DrawRect(GetCollisionRect(), 255, 0, 0);
}

bool CEnemy::CheckCollision(IBaseInterface* IBase)
{
	if(IBase->GetType() == OBJ_PISTOL_BULLET) // If shot by pistol bullet
	{
		CPistolBullet* tempb = (CPistolBullet*) IBase;
		if(tempb->GetBulletType() == CPistolBullet::BLT_PLAYER)
		{
			RECT tempr;
			if(IntersectRect(&tempr, &this->GetRect(), &tempb->GetCollisionRect()))
			{
				SetHealth(GetHealth() - 5);
				if(GetHealth() <= 0)
				{
					CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreatePistolDropMessage(this));
				}
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nHitID);
				CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyPlayerBulletMessage(tempb));
				return true;
			}
		}
	}
	else if(IBase->GetType() == OBJ_SHOTGUN_BULLET) // If shot by shotgun bullet
	{
		CShotgunBullet* tempb = (CShotgunBullet*)IBase;
		if(tempb->GetBulletType() == CShotgunBullet::BLT_PLAYER)
		{
			RECT tempr;
			if(IntersectRect(&tempr, &this->GetRect(), &tempb->GetCollisionRect()))
			{
				SetHealth(GetHealth() - 15);
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nHitID);
				CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyShotgunBulletMessage(tempb));
				return true;
			}
		}
	}
	else if(IBase->GetType() == OBJ_ROCKET_BULLET) // If shot by rocket bullet
	{
		CRocketLauncherBullet* tempb = (CRocketLauncherBullet*)IBase;
		if(tempb->isexplodng())
		{
			RECT tempr;
			if(IntersectRect(&tempr, &GetCollisionRect(), &tempb->GetDetonateRect()))
			{
				SetHealth(GetHealth() - 20);
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nHitID);
				//CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyRocketBulletMessage(tempb));

				return true;
			}
		}
	}
	else if(IBase->GetType() == OBJ_GRENADE)
	{
		CGrenade* tempb = (CGrenade*)IBase;
		
		RECT tempr;
		if(tempb->GetExploding())
		{
			if(IntersectRect(&tempr, &this->GetRect(), &tempb->GetExRadius()))
			{
				SetHealth(0);
				CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyGrenadeMessage(tempb));
				return true;
			}
		}
		
	}
	else if(IBase->GetType() == OBJ_PLAYER)
	{
		CPlayer* pTemp = CPlayer::GetInstance();

		RECT tempr;
		if(IntersectRect(&tempr, &this->GetRect(), &pTemp->GetCollisionRect()))
		{
			m_bScreenFlashing = true;
			return true;
		}
	}

	return false;
}
 
void CEnemy::Reset()
{
	health = maxHealth;
}
void CEnemy::RandomDrop()
{
	
	PICKUP temp;
	temp = PICKUP(rand()%DROP_ROCKET);
	

	if(temp == DROP_PISTOL)
	{	
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreatePistolDropMessage(this));
	}
	else if(temp == DROP_SHOTGUN)
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateShotgunDropMessage(this));
	}
	else if(temp == DROP_ROCKET)
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateRocketDropMessage(this));
	}
	else if(temp == DROP_HEALTH)
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateHealthDropMessage(this));
	}
	else if(temp == DROP_FW_GRENADE)
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateFwGrenadeDropMessage(this));
	}
	else 
		return;
}